package com.nativeringing
import android.content.Context
import android.media.AudioManager
import android.media.RingtoneManager
import android.media.ToneGenerator
import android.net.Uri
import com.facebook.react.bridge.ReactApplicationContext
import com.nativemodules.NativeRingingSpec

class NativeRinging(applicationContext: ReactApplicationContext):NativeRingingSpec(applicationContext) {
    private var ringtone: android.media.Ringtone? = null
    init {
         val audioManager: AudioManager = applicationContext.getSystemService(Context.AUDIO_SERVICE) as AudioManager
    }
    override fun ringing(index: Double) {
        try {
            // Stop any currently playing ringtone
            ringtone?.stop()
            // Get the default ringtone URI
            val toneGen = when (index.toInt()) {
                1 -> ToneGenerator(AudioManager.STREAM_MUSIC, 100)
                2 -> ToneGenerator(AudioManager.STREAM_MUSIC, 100)
                else ->ToneGenerator(AudioManager.STREAM_MUSIC, 100)
            }
            toneGen.startTone(ToneGenerator.TONE_PROP_BEEP, 150) // 150ms
            // Play the ringtone
        } catch (e: Exception) {
            // Handle any errors
            e.printStackTrace()
        }
    }

    override fun getName() = NAME
    companion object{
        const val NAME="NativeRinging"
    }
}